//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.pc_rfid.api;

public class PC_COM extends PC_COMM {
    private static final String TAG = "PC_COM";
    private SerialPort COM = null;

    public PC_COM() {
        this.isOpen = false;
    }

    public PC_COM(String path, int baud, String format) {
        this.open(path, baud, format);
    }

    public int open(String path, int baud, String format) {
        int Bits = Integer.parseInt(format.substring(0, 1));
        char Event = format.charAt(1);
        int Stop = Integer.parseInt(format.substring(2, 3));
        this.COM = new SerialPort(path, baud, Bits, Event, Stop);
        if (this.COM.isOpen) {
            this.isOpen = true;
            return 0;
        } else {
            return -1;
        }
    }

    public int Send(byte[] data) {
        if (!this.isOpen) {
            return -1;
        } else {
            this.clearBuffer();
            int len = (data[1] & 255) + 1;
            return this.COM.write(data, len);
        }
    }

    public int Receive(byte[] data) {
        if (!this.isOpen) {
            return -1;
        } else {
            byte[] data1 = new byte[256];

            long time_1;
            for(long time_2 = time_1 = System.currentTimeMillis(); time_2 - time_1 < 2000L; time_2 = System.currentTimeMillis()) {
                int len = this.COM.read(data1);
                if (this.ParsePack(data1, len, data)) {
                    int l = (data[1] & 255) + 1;
                    return l;
                }
            }

            return 0;
        }
    }

    public int close() {
        this.COM.Close();
        this.isOpen = false;
        return 0;
    }

    public void clearBuffer() {
        this.clearParse();
        this.COM.clearBuffer();
    }

    public int Send_Receive(RFdata data) {
        if (!this.isOpen) {
            return -1;
        } else {
            int a = this.Send(data.SendData);
            if (a == 0) {
                a = this.Receive(data.RecvData);
            }

            return a;
        }
    }
}
